using Godot;
using System;

namespace Box;
public partial class ProcessTreeEditor : PanelContainer
{
	public static ProcessTreeEditor CurrentEditor;
	public static string ProjectName;
	public static string ProjectPath;

	protected static string project_name;

	TreeTabContainer TreeTabContainer;
	PropertyPanel NodePropertyPanel;
	PropertyPanel TreePropertyPanel;
	FileMenu FileMenu;
	ProjectMenu ProjectMenu;

	StringPropertyControl TreeNameProperty;
	Label CurrentProjectNameLabel;

	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		TreeTabContainer = GetNode<TreeTabContainer>("VBoxContainer/HBoxContainer2/TreeTabContainer");
		NodePropertyPanel = GetNode<PropertyPanel>("VBoxContainer/HBoxContainer2/VSplitContainer/NodePropertyPanel");
		TreePropertyPanel = GetNode<PropertyPanel>("VBoxContainer/HBoxContainer2/VSplitContainer/TreePropertyPanel");
		FileMenu = GetNode<FileMenu>("VBoxContainer/PanelContainer/GridContainer/FileMenu");
		ProjectMenu = GetNode<ProjectMenu>("VBoxContainer/PanelContainer/GridContainer/ProjectMenu");
		CurrentProjectNameLabel = GetNode<Label>("VBoxContainer/PanelContainer/GridContainer/CurrentProjectNameLabel");

		TreeTabContainer.PropertyPanel = NodePropertyPanel;
		TreeTabContainer.onSelectTab += _onSelectTreeEditTab;

		ProjectMenu.TreeTabContainer = TreeTabContainer;
		ProjectMenu.ProjectOpenWindow.TreeTabContainer = TreeTabContainer;

		NodePropertyPanel.Label.Text = "节点属性";
		TreePropertyPanel.Label.Text = "树属性";
		

		TreeNameProperty = (StringPropertyControl)PropertyHelper.CreatePropertyControl(PropertyType.String,"Tree Name");

		TreePropertyPanel.Properties.AddChild(TreeNameProperty);
		TreeNameProperty.Input.TextChanged += _onSetTreeName;

		CurrentEditor = this;
	}

	public void OpenProject(string name,string path) {
		ProjectName = name;
		ProjectPath = path;

		CurrentProjectNameLabel.Text = ProjectName;

		FileMenu.CreateTaskFileWindow.RootSubfolder = $"{ProjectPath}/tasks";
		FileMenu.CreateActionFileWindow.RootSubfolder = $"{ProjectPath}/actions";
	}

	public void _onSetTreeName(string new_name) {
		ProcessTreeGraphEditor tab =  (ProcessTreeGraphEditor)TreeTabContainer.GetTabControl(TreeTabContainer.CurrentTab);
		if(new_name == "") {
			tab.Name = $"Tree{tab.GetInstanceId()}";
		} else {
			tab.Name = new_name;
		}
	}

	public void _onSelectTreeEditTab(ProcessTreeGraphEditor tab) {
		FileMenu.ProcessTreeGraphEditor = tab;
		TreeNameProperty.Input.Text = tab.Name;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}
}
